<template>
  <!-- 卸货口表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="840px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120">
      <el-row>
        <el-col :span="12">
          <el-form-item label="卸货口编号" prop="dischargeHatchCode">
            <el-input
              v-model="formData.dischargeHatchCode"
              placeholder="请输入卸货口编号"
              clearable
              style="width: 250px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="卸货口描述" prop="dischargeHatchName">
            <el-input
              v-model="formData.dischargeHatchName"
              placeholder="请输入卸货口名称"
              clearable
              style="width: 250px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="卸货口简称" prop="dischargeHatchAbbr">
            <el-input
              v-model="formData.dischargeHatchAbbr"
              placeholder="请输入卸货口简称"
              clearable
              style="width: 250px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工厂" prop="plantId">
            <el-select
              v-model="formData.plantId"
              @change="planChange"
              placeholder="请选择工厂"
              style="width: 250px"
            >
              <el-option
                v-for="item in plants"
                :key="item.id"
                :label="item.organizationName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="卸货口类型">
            <el-select
              v-model="formData.dischargeHatchType"
              placeholder="请选择卸货口类型"
              clearable
              style="width: 250px"
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList(
                  'DischargeHatchType'
                )"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="容纳卸货车辆">
            <el-input-number
              autocomplete="off"
              min="1"
              v-model="formData.vehicleNum"
              style="width: 250px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="装卸平台类型">
            <el-select
              v-model="formData.landingPlatformType"
              placeholder="请选择装卸平台类型"
              clearable
              style="width: 250px"
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList(
                  'LandingPlatformType'
                )"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用状态" prop="enabled">
            <el-radio-group v-model="formData.enabled">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-model="formData.remarks"
              placeholder="请输入备注"
              type="textarea"
              show-word-limit
              style="width: 650px"
              :maxlength="255"
              :autosize="{ minRows: 2, maxRows: 3 }"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMDISCHARGEHATCHADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  dischargeHatchCode: [
    {
      required: true,
      message: "请输入卸货口编号",
      trigger: "blur",
    },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  dischargeHatchName: [
    {
      required: true,
      message: "请输入卸货口名称",
      trigger: "blur",
    },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  dischargeHatchAbbr: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  plantId: [{ required: true, message: "请选择工厂", trigger: "change" }],
  plantCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  plantName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  enabled: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  dischargeHatchCode: "",
  dischargeHatchName: "",
  dischargeHatchAbbr: "",
  plantId: "",
  plantCode: "",
  plantName: "",
  dischargeHatchType: "",
  vehicleNum: 1,
  landingPlatformType: "",
  enabled: 1,
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        dischargeHatchCode,
        dischargeHatchName,
        dischargeHatchAbbr,
        plantId,
        plantCode,
        plantName,
        dischargeHatchType,
        vehicleNum,
        landingPlatformType,
        enabled,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        dischargeHatchCode,
        dischargeHatchName,
        dischargeHatchAbbr,
        plantId,
        plantCode,
        plantName,
        dischargeHatchType,
        vehicleNum,
        landingPlatformType,
        enabled,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      });
    }
    getPlants();
  }
);
// 工厂
let plants = ref([]);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.dischargeHatchCode = "";
  formData.dischargeHatchName = "";
  formData.dischargeHatchAbbr = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.plantName = "";
  formData.dischargeHatchType = "";
  formData.vehicleNum = 1;
  formData.landingPlatformType = "";
  formData.enabled = 1;
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

// 工厂查询
async function getPlants() {
  const data = await request({
    url: "system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plants.value = data.data;
}

function planChange() {
  const plant = plants.value.find((item) => {
    return item.id === formData.plantId;
  });
  formData.plantCode = plant.organizationCode;
  formData.plantName = plant.organizationName;
}

const loading = ref(false);

/** 卸货口表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mdm/mdmdischargehatch/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

onMounted(() => {
  getPlants();
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
